package org.mms.service.impl;

import org.mms.exception.BusinessException;
import org.mms.pojo.entity.Material;
import org.mms.pojo.entity.MaterialInbound;
import org.mms.pojo.entity.User;
import org.mms.pojo.request.MaterialInboundRequest;
import org.mms.pojo.request.MaterialInboundReviewRequest;
import org.mms.repository.MaterialRepository;
import org.mms.repository.InboundRepository;
import org.mms.repository.UserRepository;
import org.mms.service.InboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InboundServiceImpl implements InboundService {
    @Autowired
    private InboundRepository inboundRepository;
    @Autowired
    private MaterialRepository materialRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public List<MaterialInbound> list() {
        return inboundRepository.findAll();
    }

    @Override
    public MaterialInbound getById(Long id) {
        return inboundRepository.findById(id)
                .orElseThrow(() -> new BusinessException("入库记录不存在"));
    }

    @Override
    public MaterialInbound create(MaterialInboundRequest request) {
        if (inboundRepository.existsByInboundNumber(request.getInboundNumber())) {
            throw new BusinessException("入库编号已存在");
        }
        Material material = materialRepository.findById(request.getMaterialId())
                .orElseThrow(() -> new BusinessException("物资不存在"));
        User operator = userRepository.findById(request.getOperatorId())
                .orElseThrow(() -> new BusinessException("操作员不存在"));

        MaterialInbound entity = new MaterialInbound();
        entity.setInboundNumber(request.getInboundNumber());
        entity.setMaterial(material);
        entity.setQuantity(request.getQuantity());
        entity.setStatus("待审核");
        entity.setOperator(operator);
        entity.setReviewer(null);
        entity.setExpirationDate(request.getExpirationDate());

        return inboundRepository.save(entity);
    }

    @Override
    public MaterialInbound update(MaterialInboundRequest request) {
        MaterialInbound inbound = inboundRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("入库记录不存在"));
        if (!inbound.getInboundNumber().equals(request.getInboundNumber()) &&
                inboundRepository.existsByInboundNumber(request.getInboundNumber())) {
            throw new BusinessException("入库编号已存在");
        }
        Material material = materialRepository.findById(request.getMaterialId())
                .orElseThrow(() -> new BusinessException("物资不存在"));
        User operator = userRepository.findById(request.getOperatorId())
                .orElseThrow(() -> new BusinessException("操作员不存在"));

        inbound.setInboundNumber(request.getInboundNumber());
        inbound.setMaterial(material);
        inbound.setQuantity(request.getQuantity());
        inbound.setOperator(operator);
        inbound.setExpirationDate(request.getExpirationDate());

        return inboundRepository.save(inbound);
    }

    @Override
    public MaterialInbound review(MaterialInboundReviewRequest request) {
        MaterialInbound entity = inboundRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("入库记录不存在"));
        if (!"待审核".equals(entity.getStatus())) {
            throw new BusinessException("只能审核待审核状态的记录");
        }
        User reviewer = userRepository.findById(request.getReviewerId())
                .orElseThrow(() -> new BusinessException("审核人不存在"));

        entity.setStatus(request.getStatus());
        entity.setReviewer(reviewer);
        if ("通过".equals(request.getStatus())) {
            Material material = entity.getMaterial();
            material.setQuantity(material.getQuantity() + entity.getQuantity());
            materialRepository.save(material);
        }

        return inboundRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        MaterialInbound inbound = inboundRepository.findById(id)
                .orElseThrow(() -> new BusinessException("入库记录不存在"));
        if (!"待审核".equals(inbound.getStatus())) {
            throw new BusinessException("只能删除待审核状态的记录");
        }
        inboundRepository.deleteById(id);
    }
}